package com.example.dml.proxy_pattern.static_proxy;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @description:创建切换数据源的代理类
 * @author: gaoyang
 * @date: 2021/10/26 21:59
 */
public class OrderServiceStaticProxy implements IOrderService{

    private SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");

    private IOrderService orderService;

    public OrderServiceStaticProxy(IOrderService orderService) {
        this.orderService = orderService;
    }

    @Override
    public int createOrder(Order order) {
        before();
        Long time = order.getCreateTime();
        Integer dbRouter = Integer.valueOf(yearFormat.format(new Date(time)));
        System.out.println("静态代理类自动分配到【DB_"+dbRouter+"】数据源处理数据");
        DynamicDataSourceEntry.set(dbRouter);
        orderService.createOrder(order);
        after();
        return 0;
    }

    private void before(){
        System.out.println("proxy before method");
    }
    private void after(){
        System.out.println("proxy after method");
    }
}
